package com.health.ts.utils;

import java.util.List;

/**
 * 分页计算工具类
 *
 * @author cuishusong
 * @date 2022-03-28
 */
public class PageHelper<T> {

	/**
	 * 当前页
	 */
	private int currentPage = 1;
	/**
	 * 每页记录数
	 */
	private int pageSize = 10;
	/**
	 * 总条数
	 */
	private int recordTotal = 0;
	/**
	 * 总页数
	 */
	private int pageTotal;
	/**
	 * 前一页
	 */
	private int previousPage;
	/**
	 * 下一页
	 */
	private int nextPage;
	/**
	 * 第一页
	 */
	private int firstPage = 1;
	/**
	 * 最后一页
	 */
	private int lastPage;

	/**
	 * 是否有上一页
	 */
	private boolean hasPreviousPage;

	/**
	 * 是否有下一页
	 */
	private boolean hasNextPage;

	/**
	 * 每页开始数据行号
	 */
	private int startRow;
	/**
	 * 每页结束数据行号
	 */
	private int endRow;

	/**
	 * 每页的数据内容
	 */
	private List<T> content;

	/**
	 * 构造方法
	 * 
	 * @param currentPage 当前页
	 * @param pageSize    每页记录数
	 * @param recordTotal 总记录数
	 */
	public PageHelper(int currentPage, int pageSize, int recordTotal) {
		this.currentPage = currentPage;
		this.pageSize = pageSize;
		this.recordTotal = recordTotal;
		caculate();
	}

	/**
	 * 构造方法2（用默认参数构建）
	 * 
	 * @param recordTotal 总记录数
	 */
	public PageHelper(int recordTotal) {
		this.recordTotal = recordTotal;
		caculate();
	}

	/**
	 * 计算其他属性
	 */
	public void caculate() {
		// 总页数
		this.pageTotal = this.recordTotal % this.pageSize > 0 ? this.recordTotal / this.pageSize + 1
				: this.recordTotal / this.pageSize;
		// 第一页
		this.firstPage = 1;
		// 最后一页
		this.lastPage = this.pageTotal;
		// 前一页
		if (this.currentPage > 1) {
			this.previousPage = this.currentPage - 1;
		} else {
			this.previousPage = this.firstPage;
		}
		// 下一页
		if (this.currentPage < this.lastPage) {
			this.nextPage = this.currentPage + 1;
		} else {
			this.nextPage = this.lastPage;
		}
		// 开始行号
		this.startRow = (this.currentPage * this.pageSize) - (this.pageSize - 1);
		// 结束行号
		this.endRow = this.currentPage * this.pageSize;
		// 当结束行号>总行数时，结束行号=总行数
		if (this.endRow > recordTotal) {
			this.endRow = recordTotal;
		}
		this.hasNextPage = this.currentPage <= this.lastPage;
		this.hasPreviousPage = this.currentPage >= this.firstPage;
	}

	/**
	 * 翻上一页
	 */
	public void previousPage() {
		if (this.currentPage > this.firstPage) {
			this.currentPage = this.previousPage;
			caculate();
		} else {
			this.hasPreviousPage = false;
		}
	}

	/**
	 * 翻下一页
	 */
	public void nextPage() {
		if (this.currentPage < this.lastPage) {
			this.currentPage = this.nextPage;
			caculate();
		} else {
			this.hasNextPage = false;
		}
	}
	
	/**
	 * 设置分页数据内容
	 *
	 * @param content
	 */
	public void setContent(List<T> content) {
		this.content = content;
	}

	// 放开私有属性
	public int getCurrentPage() {
		return currentPage;
	}

	public int getPageSize() {
		return pageSize;
	}

	public int getPageTotal() {
		return pageTotal;
	}

	public int getRecordTotal() {
		return recordTotal;
	}

	public int getPreviousPage() {
		return previousPage;
	}

	public int getNextPage() {
		return nextPage;
	}

	public int getFirstPage() {
		return firstPage;
	}

	public int getLastPage() {
		return lastPage;
	}

	public boolean isHasPreviousPage() {
		return hasPreviousPage;
	}

	public boolean isHasNextPage() {
		return hasNextPage;
	}

	public List<T> getContent() {
		return content;
	}

	public int getStartRow() {
		return startRow;
	}

	public int getEndRow() {
		return endRow;
	}

	@Override
	public String toString() {
		return "PageHelper [currentPage=" + currentPage + ", pageSize=" + pageSize + ", recordTotal=" + recordTotal
				+ ", pageTotal=" + pageTotal + ", previousPage=" + previousPage + ", nextPage=" + nextPage
				+ ", firstPage=" + firstPage + ", lastPage=" + lastPage + ", hasPreviousPage=" + hasPreviousPage
				+ ", hasNextPage=" + hasNextPage + ", startRow=" + startRow + ", endRow=" + endRow + "]";
	}
}